using System;
using System.Diagnostics;
using Newtonsoft.Json.Linq;

namespace Orbitone.MeetingRooms.RoomFinder
{
    public class TokenRestApi
    {
        public JObject getTokens(string clientID, string redirectURI, string clientSecret, string refreshToken, string authorizationCode)
        {
            string getTokensURL;

            if (!string.IsNullOrEmpty(authorizationCode))
            {
                getTokensURL =  Globals.ENDPOINT_OAUTH + Globals.ENDPOINT_PATH_TOKEN +
                                Globals.PARAM_CLIENT_ID + clientID +
                                Globals.PARAM_REDIRECT_URI + redirectURI +
                                Globals.PARAM_CLIENT_SECRET + clientSecret +
                                Globals.PARAM_CODE + authorizationCode +
                                Globals.PARAM_GRANT_TYPE + Globals.PARAM_GRANT_TYPE_AUTHORIZATION_CODE;
            }
            else if (!string.IsNullOrEmpty(refreshToken))
            {
                getTokensURL =  Globals.ENDPOINT_OAUTH + Globals.ENDPOINT_PATH_TOKEN +
                                Globals.PARAM_CLIENT_ID + clientID +
                                Globals.PARAM_REDIRECT_URI + redirectURI +
                                Globals.PARAM_CLIENT_SECRET + clientSecret +
                                Globals.PARAM_REFRESH_TOKEN + refreshToken + 
                                Globals.PARAM_GRANT_TYPE + Globals.PARAM_GRANT_TYPE_REFRESH_TOKEN;
            }
            else
            {
                throw new Exception("Error retrieving tokens. Cannot determine whether to use refresh token or verification code.");
            }

            Trace.WriteLine("Request URL for token(s): " + getTokensURL);

            var request = new BReq();
            var json = request.HttpGet(getTokensURL);

            var decodedJSON = JObject.Parse(json);

            var error = decodedJSON["error"];
            if (error != null)
            {

                Trace.WriteLine("Error retrieving data from REST API: ");

                if (error["message"] != null)
                {
                    Trace.WriteLine(error["message"]);
                }
                else
                {
                    Trace.WriteLine(error["message_description"]);
                }

                return null;
            }

            return decodedJSON;
        }

        public JObject callRestApi(string accessToken, string restPath, int requestMethod, string headers, string methodData)
        {
            var restApiURL = Globals.ENDPOINT_REST_API + Globals.ENDPOINT_REST_API_VERSION +
                             restPath + Globals.PARAM_ACCESS_TOKEN + accessToken;

            Trace.WriteLine("Request URL for REST API: " + restApiURL);

            string json;
            var request = new BReq();

            if (requestMethod.Equals(Globals.REST_API_GET))
            {
                json = request.HttpGet(restApiURL);
            }
            else if (requestMethod.Equals(Globals.REST_API_POST))
            {
                json = request.HttpPost(restApiURL, methodData);
            }
            else
            {
                throw new Exception("Error calling REST API. Cannot determine whether to use GET or POST request method.");
            }

            var decodedJSON = JObject.Parse(json);

            var error = decodedJSON["error"];
            if (error != null) {
    
                Trace.WriteLine("Error retrieving data from REST API: ");

                if (error["message"] != null) {
                    Trace.WriteLine(error["message"]);
                }
                else {
                    Trace.WriteLine(error["message_description"]);
                }
        
                return null;
            }
            return decodedJSON;
        }
    }
}